package com.jin.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.jin.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    private String timeout = "1m";
    //在支付宝创建的应用的id
    private   String app_id = "2021000117695111";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCDtJNWu1+1w8/N6yPvlQZ9OGZ7Bq3dSga5+ofBZM3Y76L5qpbwXWCNNHabU9MeBR5GRwlcxaUW97ZKZHfZaZtRfQpAl6UNhVs9mHqUvCy6lCVkpPh4vBDQFWSZYrOpA0gOxnto1Gnvw+bMXxf/m0VZ1ggUgaktllnQ2bBZWXYeChEOsqrnKyXuMnk/BNE3ge/FIP0O2e1dwqGz8oIpV0wA8qUjzrB/ObSqhC80/TevEyhHENL9SB0yKDEBAmIBQ3PVPWfUCQv/crowoBWVWPmoVbRmjNrVa4GGkVeo9nRl18UBmeGGHcgaSssLBlL1G2o5HnNcpj8w/iVaZQzHR65bAgMBAAECggEAbsXlGpC0GjvcPtRjwDE2aQGT6Cug9js51xhpL/apz6kWCSk6lGXlPD6A93mUWm5xnShILpgpl1y3HHDOALV7JhmmEte4wduE9xSVHR37/N13aeP6/Akpluo3V28VrrSM2aiEyZHGnBvHteLRW3Hr7XhACYdYlHgdiLRfR3cgCniwfMzEWzEoL/c2SUU4R7ZH3Lhh9LP1G87l9CTxWMWU8bzmfDikvHZrqc58kcxeZTGgExFptk+5fFxIBkzRFbRAk+ifJjz6UCHpCp5JlgeRC/syXZyHktyUZhzhOr60uPqNOhTdOI+KyAeRtj9iJvSxdD9VXoyNs2VNMhIKNqC4kQKBgQDeWDakcemgHceFplGyjFH1Bh5xLPsbacbFTFiDtdgiL6QfULUC3+ZrU2B/u4K/hs7bdpqz6HM8iACYIpgzpu9xJUmEwp4xOPbk9mK6GCCiisyTeEk5tnZIQI5ZibloYABrYx3QKOCQ6AsmqZIWEMXl+pAQ6Pj/UXy6sS750PeRAwKBgQCXpB6fVxficda9lwoQTwqV1+z2CY1LYn7eiBbO1+4/aYjavt53xoljPtIs/aWfoafO5OFYlYYbKNpfxfyA47EfkdQiKXEhk905Qlxvr5Pr46SaVZd3yguBiTftWAQDL6Xu9+uHwWUo7k9LcVGSG+/JCoNJUmtI8sUrhQWC/CHxyQKBgQCNGqB61gv04FE6ASYB0MlGXX6+RlSJviQ2G9BkreefMmrOsNJfI6bAX+ZyyZYdZDksz+U33YIcXRc7dWDwDrYQMSCqDzSDemo9PhvDjAJSBRH1WLXn2WLRUZ7FNgEjOyGpBPrrmrWBYLGK9LxQIo7k95GsGZ3Dsnwcost0IOir7wKBgAOlhrb+OSzQ4AEfKLbLr816q9Ae2q79Z1+ilinTN39+4jOgc2kd9R/V2ugKEXmNQaPQDTs8HUV57gZvlGrlDtVbhGZt6LKOA7vkxKRK7Ju3lbXAaSwQR8aNrjKe8znAY5UjkR5qoCwjzvRZfUSuN032WsHgoYPXkrZBNKi5t3dBAoGBANzGQ8uSrg224ZTEjgtG1rIacU0bxJeiR26m44NMCOHkEu5c+pyYS8S51denm9y/yxngfpYscoYzZciVgaGQpQ1BqQckGn7HuZ2dqIyZtcQgy5d0BXW3aXLUgzlwjn73cKhrN34HwqB1txXPOZ7KryQW2nwhSyb57BVLMeCW82IQ";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjIRi5PnuJ8iPjjafAzDV3arkBE9gEh8d2YQW/BX3jclTF8aM2oLjHQISge9PZOsumsQzYuIeWPHhdjIBQpBQAmo+WzhA/2XppgYYiA2fFDKxwZ11dKL3dpdK6uKUkM5S8TyqWeLjZFOL1vb5RCFk91jzA59Zsm9a+8JGDgV5LAzRWOhfzYjTyo+JcDKWorjpenwErqLG+1WllQ24x9ppBoB+tDWoBDXXsSEhfA1VCLfWFlWMl43Sy9vAlT+O/s11wbFVgkz+gXRMZihocgQoFem+n/xO9JXgWy890TxMS/tPerYZ1iYFYFJqKmqnh0K06STFwa2OYE6kY0Co/OrRpwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://ho2vb2nakj.cdhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;
    }
}
